<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="static/easyui/jquery.min.js"></script>
<script type="text/javascript" src="static/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="static/easyui/locale/easyui-lang-zh_CN.js"></script>
<link rel="stylesheet" href="static/easyui/themes/metro/easyui.css" />
<link rel="stylesheet" href="static/easyui/themes/icon.css" />
<title>药房退药 </title>
</head>
<body>
<script type="text/javascript">
$(function(){
	  //退药信息
 	 $('#returnGoodsRecord-tabs').datagrid({
		    url:'select-returnGoodsRecord',
		    pagination:true,
		    pageNumber:1,
		    pageSize:10,
		    pageList:[10,9,8],
		    columns:[[
	    		{field:'return_goods_type',title:'退药部门',width:100,
	    			formatter:function(value,row,index){
						//退药状态为0
						if(value=="门诊药房退药"){
							return "门诊";
						}
						if(value=="住院药房退药"){
							return "住院";
						}
					}	
	    		},
	    		{field:'return_goods_id',title:'退药单号',width:103.2},
				{field:'return_goods_allquantity',title:'退药总数',width:103.2},
				{field:'return_goods_total',title:'退药总金额',width:102},
				{field:'return_goods_reason',title:'退药原因',width:260},
				{field:'return_goods_time',title:'退药时间',width:150,formatter:comFormatDate},
				{field:'return_goods_state',title:'退药情况',width:93.2,
						formatter:function(value,row,index){
							//退药状态为0
							if(value=="4"){
								return "待退药";
							}
							if(value=="5"){
								return "已退药";
							}
						}	
				},
				{field:'returndrug_work',title:'操作',width:103.2,align:'center',
						formatter:function(value,row,index){
							if(row.return_goods_state=="4"){
							  return "<a  class='returngoods-btn  easyui-linkbutton' onclick='return_goods("+row.return_goods_id+")'>查看记录</a>";

							}
							if(row.return_goods_state=="5"){
								  return "<a  class='destorygoods-btn  easyui-linkbutton' onclick='destroy_goods("+row.return_goods_id+")'>查看记录</a>";
							}
					    }	
			   }
		    ]],
		    onLoadSuccess:function(data){
				var start = 0;
				var end = 0;
				var mer = ['return_goods_type'];//需要合并的字段，即field
				//有数据就合并----对应数据的数量
				if (data.total > 0){
					//console.log("data.total:"+data.total);
				    var temp = data.rows[0].return_goods_type;   //需要比较的列-----排班类型----门诊还是住院
				    for (var i = 1; i < data.rows.length; i++) {
					    //第一列的门诊或住院类型=遍历过程中的门诊或住院
						if (temp == data.rows[i].return_goods_type) {
							 //console.log("if----temp:"+temp+"------data.rows[i].section_type:"+data.rows[i].section_type);
						     end++;
						} else {
						    if (end> start) {
							   //console.log("end > start----------start:"+start+"------end:"+end);
								for(var j = 0; j < mer.length; j ++){
									$('#returnGoodsRecord-tabs').datagrid('mergeCells', {
										index: start,//起始索引
										rowspan: end - start + 1,  //需要合并的行数
										field: mer[j]
									});
								}
						   }
						    temp = data.rows[i].return_goods_type;
						    start = i;
						    end = i;
						    //console.log("else----temp:"+temp+"------data.rows[i].section_type:"+data.rows[i].section_type
						    						// +"------start:"+start+"------end:"+end);
					 }
				 }
				    //这里是为了判断重复内容出现在最后的情况
				    if (end > start) {
						for(var j = 0; j < mer.length; j ++){
							$('#returnGoodsRecord-tabs').datagrid('mergeCells', {
								index: start,
								rowspan: end - start + 1,
								field: mer[j]
							});
						}
				    }
				}/* 最大if结束 */
		    }
		});
	  
 	 $("#search-returndrugstartDate").searchbox({
			searcher:function(value){
				//alert(value);
				//访问数据源、初始化表格
		   $('#returnGoodsRecord-tabs').datagrid({
			    url:'select-returnGoodsRecord',
			    queryParams:{
			    	return_goods_id:value
			    },
			    pagination:true,
			    pageNumber:1,
			    pageSize:10,
			    pageList:[10,9,8],
			    columns:[[
		    		{field:'return_goods_type',title:'退药类型',width:100,
		    			formatter:function(value,row,index){
							//退药状态为0
							if(value=="药房门诊退药"){
								return "门诊";
							}
							if(value=="药房住院退药"){
								return "住院";
							}
						}	
		    		},
		    		{field:'return_goods_id',title:'退药单号',width:103.2},
					{field:'return_goods_allquantity',title:'退药总数',width:203.2},
					{field:'return_goods_total',title:'退药总金额',width:152},
					{field:'return_goods_time',title:'退药时间',width:150,formatter:comFormatDate},
					{field:'return_goods_state',title:'退药状态',width:103.2,
							formatter:function(value,row,index){
								//退药状态为0
								if(value=="4"){
									return "待退药";
								}
								if(value=="5"){
									return "已退药";
								}
							}	
					},
					{field:'returndrug_work',title:'操作',width:203.2,align:'center',
							formatter:function(value,row,index){
								 return "<a  class='returngoods-btn  easyui-linkbutton' onclick='return_goods("+row.return_goods_id+")'>查看记录</a>";
						    }	
				   }
			    ]],
			    onLoadSuccess:function(data){
					var start = 0;
					var end = 0;
					var mer = ['return_goods_type'];//需要合并的字段，即field
					//有数据就合并----对应数据的数量
					if (data.total > 0){
						//console.log("data.total:"+data.total);
					    var temp = data.rows[0].return_goods_type;   //需要比较的列-----排班类型----门诊还是住院
					    for (var i = 1; i < data.rows.length; i++) {
						    //第一列的门诊或住院类型=遍历过程中的门诊或住院
							if (temp == data.rows[i].return_goods_type) {
								 //console.log("if----temp:"+temp+"------data.rows[i].section_type:"+data.rows[i].section_type);
							     end++;
							} else {
							    if (end> start) {
								   //console.log("end > start----------start:"+start+"------end:"+end);
									for(var j = 0; j < mer.length; j ++){
										$('#returnGoodsRecord-tabs').datagrid('mergeCells', {
											index: start,//起始索引
											rowspan: end - start + 1,  //需要合并的行数
											field: mer[j]
										});
									}
							   }
							    temp = data.rows[i].return_goods_type;
							    start = i;
							    end = i;
							    //console.log("else----temp:"+temp+"------data.rows[i].section_type:"+data.rows[i].section_type
							    						// +"------start:"+start+"------end:"+end);
						 }
					 }
					    //这里是为了判断重复内容出现在最后的情况
					    if (end > start) {
							for(var j = 0; j < mer.length; j ++){
								$('#returnGoodsRecord-tabs').datagrid('mergeCells', {
									index: start,
									rowspan: end - start + 1,
									field: mer[j]
								});
							}
					    }
					}/* 最大if结束 */
			    }
			 });
			}
 	 });
 	 
 	 //确定退药
 	 $(".return_goods_btn").click(function(){
 		 //销毁
 		 var return_goodsData=$("#returndrugDetails-tabs").datagrid("getRows");
 		 var return_id=return_goodsData[0].return_goods_id;
 		$("<form method='post' id='for'>").appendTo("body").form({
			url:'save-Destructions?return_id='+return_id,				
			onSubmit:function(param){
				//退药原因
				param["destroy.destroyReason"] ="过期";
				
				//退药状态4----为门诊销毁
				param["destroy.destroyState"] = 4;
				
				//获取负责人
				param["destroy.destroyCheckpeople"] =$("#applypeople").val();
				
				//获取表格中的详情记录
				var rows = $("#returndrugDetails-tabs").datagrid("getRows");

				var total=0;
				for (var i = 0; i < rows.length; i++){
					//药品库存编号
					param["destrydetail["+i+"].inventory.inventoryNumber"] = rows[i]["drug_number"];
					//单个药品的销毁数量                                                          
					param["destrydetail["+i+"].destroyDetailsQuantity"] =rows[i]["return_details_quantity"]; 
					//药品批次
					param["destrydetail["+i+"].destroyPiCi"] = rows[i]["return_details_pici"];
					//销毁药品的总数量
					total +=parseInt(rows[i]["return_details_quantity"]); 
				}
			
				//总数量
				param["destroy.destroyAllquantity"] = parseInt(total);
			},
			success:function(data){
				if(parseInt(data)==1){
					$(".window-apply").text("你的药品已经销毁成功！！");
					$("#look-returnGoods-win").window("close");
					$("#applysaveclinic-win").window('open');
			    	//三秒之后自动消失
				   setTimeout(function(){
					   $("#applysaveclinic-win").window('close');
					  }, 3000);
			    	
				   $('#forYesdrug').form("clear");//清空文本
				   $("#returnGoodsRecord-tabs").datagrid("reload");
				}
				
				if(parseInt(data)==2){
				     $(".window-apply").text("销毁失败,稍后重试......");
			    	 $("#applysaveclinic-win").window('open');
			    	 $("#look-returnGoods-win").window("close");
			    	//三秒之后自动消失
				   setTimeout(function(){
				    	 $("#applysaveclinic-win").window('close');
					  }, 3000);
			    	
				   $('#forYesdrug').form("clear");//清空文本
				   $("#returnGoodsRecord-tabs").datagrid("reload");
				}
			}
		}).submit();
 	 });
 	 
});



//退药查看详情
function return_goods(return_goods_id){
   //打开窗口
 $("#look-returnGoods-win").window('open');
 $('#returndrugDetails-tabs').datagrid({
	    url:'select_returnDrug_recordDetails?return_goods_id='+return_goods_id,
	    pagination:true,
	    pageNumber:1,
	    pageSize:6,
	    pageList:[6,7],
	    columns:[[
    		{field:'return_details_pici',title:'药品批次',width:79.8},
			{field:'drug_number',title:'药品编号',width:176.9},
			{field:'drug_name',title:'药品名称',width:100.9},
			{field:'drug_retail_price',title:'药品零售价',width:88.9},
			{field:'drug_standard',title:'药品规格',width:88.9},
			{field:'drug_unit',title:'单位',width:100.9},
			{field:'return_goods_id',title:'退药主表编号',hidden:true},
			{field:'inventory_details_stardate',title:'生产日期',width:135.9,formatter:comFormatDate2},
			{field:'inventory_details_usedate',title:'有效期',width:146.9},
			{field:'return_details_quantity',title:'退药数量',width:86.8}
	    ]]
 });
}

//销毁详情
function  destroy_goods(return_goods_id){
	  //打开窗口
	 $(".return_goods_btn").hide();
	 $("#look-returnGoods-win").window('open');
	 $('#returndrugDetails-tabs').datagrid({
		    url:'select_returnDrug_recordDetails?return_goods_id='+return_goods_id,
		    pagination:true,
		    pageNumber:1,
		    pageSize:6,
		    pageList:[6,7],
		    columns:[[
	    		{field:'return_details_pici',title:'药品批次',width:79.8},
				{field:'drug_number',title:'药品编号',width:176.9},
				{field:'drug_name',title:'药品名称',width:100.9},
				{field:'drug_retail_price',title:'药品零售价',width:88.9},
				{field:'drug_standard',title:'药品规格',width:88.9},
				{field:'drug_unit',title:'单位',width:100.9},
				{field:'return_goods_id',title:'退药主表编号',hidden:true},
				{field:'inventory_details_stardate',title:'生产日期',width:135.9,formatter:comFormatDate2},
				{field:'inventory_details_usedate',title:'有效期',width:146.9},
				{field:'return_details_quantity',title:'退药数量',width:86.8}
		    ]]
	 });
	}


//日期格式化
	function comFormatDate(date) {
		var day = new Date(date); 
		var Year = 0; 
		var Month = 0; 
		var Day = 0; 
		var CurrentDate = ""; 
		//初始化时间 
		Year= day.getFullYear();//ie火狐下都可以 
		Month= day.getMonth()+1; 
		Day = day.getDate(); 
		Hour = day.getHours(); 
		 Minute = day.getMinutes(); 
		 Second = day.getSeconds(); 
		CurrentDate += Year + "-"; 
		if (Month >= 10 ) { 
			CurrentDate += Month + "-"; 
		}else { 
			CurrentDate += "0" + Month + "-"; 
		} 
		if (Day >= 10 ) { 
			CurrentDate += Day ; 
		}else { 
			CurrentDate += "0" + Day ; 
		} 
		if(Hour>=10){
			CurrentDate +=" "+Hour;
		}else{
			CurrentDate +=" "+"0"+Hour;
		}
		if(Minute>=10){
			CurrentDate +=":"+Minute;
		}else{
			CurrentDate +=":"+"0"+Minute;
		}
		if(Second>=10){
			CurrentDate +=":"+Second;
		}else{
			CurrentDate +=":"+"0"+Second;
		}
		return CurrentDate; 
	}
	
	//格式化时间
	function comFormatDate2(date) {
		var day = new Date(date); 
		var Year = 0; 
		var Month = 0; 
		var Day = 0; 
		var CurrentDate = ""; 
		//初始化时间 
		Year= day.getFullYear();//ie火狐下都可以 
		Month= day.getMonth()+1; 
		Day = day.getDate(); 
		CurrentDate += Year + "-"; 
		if (Month >= 10 ) { 
			CurrentDate += Month + "-"; 
		}else { 
			CurrentDate += "0" + Month + "-"; 
		} 
		if (Day >= 10 ) { 
			CurrentDate += Day ; 
		}else { 
			CurrentDate += "0" + Day ; 
		} 
		
		return CurrentDate; 
	}
</script>
 <!-- 面板	 -->
 <fieldset style="border:1px solid #A6A6A6;border-radius:6px;width:1050px;overflow: hidden;margin:0 auto;margin-top:10px;">
   <legend style="font-size:17px">药房退药信息</legend>
       <a style="margin-left:14px;">
		<input id="search-returndrugstartDate" name="search-checkdrugname" 
		class="easyui-textbox"  data-options="width:160,height:30,prompt:'输入退药时间或者退药单号' ">
	  </a>			       
    <div style="margin-left:15px;margin-top:8px;">
     	 <table id="returnGoodsRecord-tabs" class="easyui-datagrid" data-options="fitColumns:false,singleSelect:true,width:1016,height:360" >
	  
	    </table>
</div>	<!-- tab盒子 -->

<!-- 查看详情弹框 -->
  <div id="look-returnGoods-win" class="easyui-window" title="请领详情记录" style="width:1015px;height:475px;"
  data-options="iconCls:'icon-save',closed:true,modal:true,shadow:true,resizable:false,collapsible:false,minimizable:false,maximizable:false">
 
    <div style="">
           <table id="returndrugDetails-tabs" class="easyui-datagrid" data-options="fitColumns:false,singleSelect:true,width:1000,height:359" >
 
           </table>
            <p style="margin-left:45%;margin-top:30px;">
                  <a  class='return_goods_btn easyui-linkbutton'  iconCls="icon-ok">确定</a>
             </p>	
    </div>
    
</div>
									       
</fieldset>


<!-- 弹框提示 -->
<div id="applysaveclinic-win" class="easyui-window"  title="提示框" style="width:300px;height:156px"
    data-options="iconCls:'icon-save',modal:true,resizable:false,collapsible:false,maximizable:false,minimizable:false,closed:true">
           <p class="window-apply" style=" margin-left: 72px;font-size: 16px;margin-top: 47px;">
          </p>
</div>

<input type="hidden" value="${user.userName}"  id="applypeople">
</body>
</html>